package presentation;


import java.awt.BorderLayout;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.io.File;

public class Browser extends JFrame implements HyperlinkListener {
  public Browser() {

      String slash = System.getProperty("file.separator");
       File file = new File("aux");
       String jarPath = file.getAbsolutePath();
       String jarPath2 = System.getProperty("java.class.path");
       int lastDot = jarPath2.lastIndexOf(";");
       String aux = jarPath2;
       if (lastDot != -1) {
            aux = jarPath2.substring(0, lastDot);
       }
       int lastSlash = aux.lastIndexOf(slash);
       String path = aux.substring(0,lastSlash+1);
       if (path.length() == 0) {
	   lastDot = jarPath.lastIndexOf(";");
	   aux = jarPath;
	   if (lastDot != -1) {
	    aux = jarPath.substring(0, lastDot);
	    }
	    lastSlash = aux.lastIndexOf(slash);
	    path = aux.substring(0,lastSlash+1);
       };
      
	  char[] c = path.toCharArray();

      String path2 = "";

	  if (slash.equals("\\")){
		  for (int i = 0; i < path.length(); ++i) {
			   if (c[i] == '\\') {
					c[i] = '/';
			   }
		  }
	  
	  for (int i = 0; i < path.length(); ++i) {
			 path2 = path2 + c[i];
	  }
      }
      else path2 = path;
	  startBrowser("file:///" + path2 + "files" + "/" + "start.html");
  }

  private JEditorPane htmlPane;
  private void startBrowser(String initialURL) {

    try {
        htmlPane = new JEditorPane(initialURL);
        htmlPane.setEditable(false);
        htmlPane.addHyperlinkListener(this);
        JScrollPane scrollPane = new JScrollPane(htmlPane);
        getContentPane().add(scrollPane, BorderLayout.CENTER);
    } catch(IOException ioe) {
     //exception
    }

	setMaximumSize(new java.awt.Dimension(800, 600));
    setMinimumSize(new java.awt.Dimension(800, 600));
	setResizable(false);
	setTitle("Help");
	setLocationRelativeTo(null);
    setVisible(true);
  }

  public void hyperlinkUpdate(HyperlinkEvent event) {
    if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
      try {
        htmlPane.setPage(event.getURL());
      } catch(IOException ioe) {
        warnUser("Can't follow link to "
                 + event.getURL().toExternalForm() + ": " + ioe);
      }
    }
  }

  private void warnUser(String message) {
    JOptionPane.showMessageDialog(this, message, "Error",
                                  JOptionPane.ERROR_MESSAGE);
  }
}
